﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Data.SystemManager
{
    public class PurchaseReconciliationRepository : RepositoryBase<PurchaseReconciliation>
    {
        public IQueryable<ReconciliationView> GetListReconciliation(int status, string financeNumber, string productName, string supplierName)
        {
            var result = from pr in dbcontext.PurchaseReconciliations
                         join pc in dbcontext.PurchaseComposes
                             on pr.PurchaseComposeId equals pc.Id
                         join pro in dbcontext.Products
                             on pc.ProductId equals pro.Id
                         join customer in dbcontext.Customers
                             on pc.SupplierId equals customer.Id
                         where (status == -10 || pr.Status == status)
                         && (pr.FinanceNumber.Contains(financeNumber))
                         && (pro.Name.Contains(productName))
                         && (customer.Name.Contains(supplierName))
                         select new ReconciliationView
                         {
                             Id = pr.Id,
                             PurchaseComposeId = pr.PurchaseComposeId,
                             Status = pr.Status,
                             ApplyTime = pr.ApplyTime,
                             ShouldAmount = pr.ShouldAmount,
                             ActualAmount = pr.ActualAmount,
                             FinanceNumber = pr.FinanceNumber,
                             Memo = pr.Memo,
                             ProductName = pro.Name,
                             SupplierName = customer.Name,
                             ReconciliationType = pr.ReconciliationType
                         };
            return result;
        }


        public ReconciliationView GetSingleReconciliation(Guid id)
        {
            var result = from pr in dbcontext.PurchaseReconciliations
                         join pc in dbcontext.PurchaseComposes
                             on pr.PurchaseComposeId equals pc.Id
                         join pro in dbcontext.Products
                             on pc.ProductId equals pro.Id
                         join customer in dbcontext.Customers
                             on pc.SupplierId equals customer.Id
                         where pr.Id == id
                         select new ReconciliationView
                         {
                             Id = pr.Id,
                             PurchaseComposeId = pr.PurchaseComposeId,
                             Status = pr.Status,
                             ApplyTime = pr.ApplyTime,
                             ShouldAmount = pr.ShouldAmount,
                             ActualAmount = pr.ActualAmount,
                             FinanceNumber = pr.FinanceNumber,
                             Memo = pr.Memo,
                             ProductName = pro.Name,
                             SupplierName = customer.Name,
                             ProductId = pro.Id,
                             CreatorName = pr.CreatorName,
                             CreateTime = pr.CreateTime,
                             StandardAmount = pc.StandardAmount,
                             Amount = pc.Amount,
                             CostPrice = pc.CostPrice,
                             TotalMoney = pr.TotalMoney,
                             ReconciliationType = pr.ReconciliationType
                         };
            return result.FirstOrDefault();
        }
    }
}
